package Commands;

import Session.TestSession;

import java.util.HashMap;

/**
 * Created by:
 * User: Igor Martynovich and Guzenko Igor
 * Date: 24.11.13
 */
public class CommandHandler {
     HashMap<CommandNames,Command> commandMap;

    public CommandHandler(){
        this.commandMap = new HashMap<>(4);
        this.commandMap.put(CommandNames.next,new CommandNext());
        this.commandMap.put(CommandNames.prev,new CommandPrev());
        this.commandMap.put(CommandNames.exit,new CommandExit());
        this.commandMap.put(CommandNames.accept,new CommandAcceptAnswer());
    }

    public void runCommand(String userInput, TestSession userSession){
           boolean isCommand = isCommand(userInput);
           commandMap.get(getCommand(userInput,isCommand)).execute(userSession);

    }

    private boolean isCommand(String userInput){
        boolean isCommand = false;
        String command = "command";
        if(userInput.length()>command.length()){
            if(command.equals(userInput.substring(0,command.length()))){
                isCommand=true;
            }
        }
        return isCommand;
    }

    private CommandNames getCommand(String userInput, boolean isCommand){
        CommandNames commandName = CommandNames.accept;
        if(isCommand){
        for(CommandNames name:CommandNames.values()){
            if(name.toString().equals(userInput.substring("command ".length(),userInput.length()))){
                commandName=name;
            }
        }
        }
        return commandName;
    }
}
